import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class BasicMessageChannelPage extends StatefulWidget {
  String title;

  BasicMessageChannelPage(this.title);

  @override
  _BasicMessageChannelPage createState() => _BasicMessageChannelPage(title);
}

class _BasicMessageChannelPage extends State<BasicMessageChannelPage> {
  String title;

  _BasicMessageChannelPage(this.title);

  String plat = "Android";
  String recive = "暂无";
  //Flutter与原生相互发送消息，用于数据传递
  late BasicMessageChannel messageChannel = const BasicMessageChannel(
      'flutter_and_native_100', StandardMessageCodec());

  //发送消息
  Future<Map> sendMessage(Map json) async {
    Map reply = await messageChannel.send(json) as Map;

    //解析 原生发给 Flutter 的参数
    int code = reply["code"];
    String message = reply["message"];

    setState(() {
      recive = "code:$code message:$message";
    });
    return reply;
  }

  //接收消息监听
  void receiveMessage() {
    messageChannel.setMessageHandler((dynamic result) async {
      //解析 原生发给 Flutter 的参数
      int code = result["code"];
      String message = result["message"];

      setState(() {
        recive = "receiveMessage: code:$code message:$message";
      });
      return 'Flutter 已收到消息';
    });
  }

  @override
  void initState() {
    super.initState();

    //平台标识
    if (Platform.isIOS) {
      //ios相关代码
      plat = "IOS ";
    } else if (Platform.isAndroid) {
      //android相关代码
      plat = "Android";
    }

    receiveMessage();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    AppBar appBar = AppBar(
      backgroundColor: Colors.blue,
      //标题居中显示
      centerTitle: true,
      //返回按钮占位
      leading: Container(),
      //标题显示
      title: Text(title),
    );
    return Scaffold(
      appBar: appBar,
      //显示的页面
      body: buildWidget(context),
    );
  }

  buildWidget(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.all(20),
          child: Text(
            "接收到的消息 $recive",
            style: const TextStyle(color: Colors.blue, fontSize: 12),
          ),
        ),
        const Padding(
          padding: EdgeInsets.all(20),
          child: Text(
            "本页面是通过 BasicMessageChannel 与原生 android ios 通信的实例",
            style: TextStyle(color: Colors.red, fontSize: 12),
          ),
        ),
        Padding(
          padding: const EdgeInsets.all(10),
          child: TextButton(
              style: ButtonStyle(
                side: MaterialStateProperty.all(
                    const BorderSide(color: Colors.grey, width: 1)),
              ),
              onPressed: () {
//                sendMessage("{'method':'test','ontent':'flutter 中的数据','code':100}");
                sendMessage(
                    {"method": "test", "ontent": "flutter 中的数据", "code": 100});
              },
              child: Text(
                "向" + plat.toString() + "发送消息",
              )),
        ),
        Padding(
          padding: const EdgeInsets.all(10),
          child: TextButton(
              style: ButtonStyle(
                side: MaterialStateProperty.all(
                    const BorderSide(color: Colors.grey, width: 1)),
              ),
              onPressed: () {
                sendMessage(
                    {"method": "test2", "ontent": "flutter 中的数据", "code": 100});
              },
              child: Text(
                "向" + plat.toString() + "发送消息",
              )),
        ),
        Padding(
          padding: const EdgeInsets.all(10),
          child: TextButton(
              style: ButtonStyle(
                side: MaterialStateProperty.all(
                    const BorderSide(color: Colors.grey, width: 1)),
              ),
              onPressed: () {
                sendMessage(
                    {"method": "test3", "ontent": "flutter 中的数据", "code": 100});
              },
              child: Text(
                "打开" + plat.toString() + "原生页面",
              )),
        ),
      ],
    );
  }
}
